package com.example.imtoken.core.utils

import android.content.Context
import com.example.imtoken.entitys.http.GasEntity
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import java.math.BigDecimal

object Utils {

    /**
     * 流布局 LayoutManager
     * @param context Context
     * @return FlexboxLayoutManager
     */
    fun getFlexboxLayoutManager(context: Context): FlexboxLayoutManager {
        //设置布局管理器
        val flexboxLayoutManager = FlexboxLayoutManager(context)
        //flexDirection 属性决定主轴的方向（即项目的排列方向）。类似 LinearLayout 的 vertical 和 horizontal:
        // 主轴为水平方向，起点在左端。
        flexboxLayoutManager.flexDirection = FlexDirection.ROW
        //flexWrap 默认情况下 Flex 跟 LinearLayout 一样，都是不带换行排列的，但是flexWrap属性可以支持换行排列:
        // 按正常方向换行
        flexboxLayoutManager.flexWrap = FlexWrap.WRAP
        //justifyContent 属性定义了项目在主轴上的对齐方式:
        // 交叉轴的起点对齐
        flexboxLayoutManager.justifyContent = JustifyContent.FLEX_START
        return flexboxLayoutManager
    }

    fun stringReplace(str: String?): String {
        if (str != null && str.length > 20) {
            val s1 = str.substring(10, str.length - 10)
            return str.replace(s1, "...")
        }
        return "error"
    }

    /**
     * 计算矿工费
     */
    fun calculate(gasEntity: GasEntity?): String {
        return if (gasEntity?.steps?.size ?: 0 > 3) {
            val price = ((gasEntity?.gas
                ?: 0) * (gasEntity?.steps?.get(gasEntity.defPosition)?.gas
                ?: 0)).toBigDecimal()
            val b = price.divide(BigDecimal("10").pow(gasEntity?.pow ?: 0))
            b.toPlainString()
        } else {
            val price = ((gasEntity?.gas
                ?: 0) * (gasEntity?.steps?.get(gasEntity.defPosition)?.gas
                ?: 0)).toBigDecimal()
            val b = price.divide(BigDecimal("10").pow(gasEntity?.pow ?: 0))
            b.toPlainString()
        }
    }

    fun calculate(gas: Int, gasPrice: Int, pow: Int): String {
        return (gas * gasPrice).toBigDecimal().divide(BigDecimal("10").pow(pow)).toPlainString()
    }

    fun initialAmountMultiply(decimal: Int, amount: String): String {
        if (amount == "0") return amount
        val big = amount.toBigDecimal()
        val s = big.multiply(BigDecimal("10").pow(decimal))
        return s.toPlainString()
    }

    fun initialAmountDivide(decimal: Int, amount: String): String {
        if (amount == "0") return amount
        val big = amount.toBigDecimal()
        val s = big.divide(BigDecimal("10").pow(decimal))
        return s.toPlainString()
    }

    /**
     * 验证助记词
     * @param mnemonic String
     */
    fun verifyMnemonic(mnemonic: String?): String? {
        val d = mnemonic?.split(" ")
        if (d?.size ?: 0 == 12) {
            val sb = StringBuffer()
            for (i in 0 until (d?.size ?: 0)) {
                if (i == 0) {
                    sb.append(d?.get(i))
                } else {
                    sb.append(" ")
                        .append(d?.get(i))
                }
            }
            return sb.toString()
        }
        return null
    }

}